var express = require('express');
var router = express.Router();

var jwt = require("../../utils/jwt")
var bcrypt = require("../../utils/bcrypt")
var mongodb = require("../../utils/mongodb")
/* GET home page. */   // /api/user个人中心
router.get('/', function(req, res, next) {
    //连接数据库
    mongodb.open({
       collectionName:"user" 
    }).then(({collection,client,ObjectId})=>{
        //查询数据
        collection.find({
            username:req.query.decode.username,  //因为app.all("/api/*") ===> 经过token认证后可以获取到username和_id
            _id:ObjectId(req.query.decode._id)
        }).toArray((err,result)=>{
            if(err){
                res.send({err:1,msg:"集合操作失败..."})
            }else{
                //判断用户是否存在
                if(result.length>0){
                    //自动登录成功 需要返回数据给前端
                    delete result[0].username
                    delete result[0].password
                    res.send({
                        err:0,
                        msg:"自动登录成功...",
                        data:result[0]
                    })
                }else{
                    res.send({err:1,msg:"自动登录失败..."})
                }
            }
            client.close()
        })
    }).catch(err=>res.send({err:1,msg:"数据库连接失败..."}))
});

module.exports = router;
